#include "../../include/api/dtuApi.h"

#include <iostream>
#include <thread>
#include <unistd.h> // 引入 usleep()

RTCM_Date g_rtcmData; // 全局变量，传递数据
int32_t g_sandRtcmState; // 向外发送RTCM数据的状态值
int32_t getRTCMdata(uint8_t *buf, uint32_t bufLen)
{
    if(g_sandRtcmState == SAND_RTCM_DATA_OK){
        g_sandRtcmState = SAND_RTCM_DATA_WAIT;
        if(bufLen < g_rtcmData.rtcmLen){
            return SAND_RTCM_DATA_ERROR;
        }
        memset(buf, 0 , bufLen);
        memcpy(buf, g_rtcmData.rtcmData, g_rtcmData.rtcmLen);
        return g_rtcmData.rtcmLen;
    }
    return SAND_RTCM_DATA_WAIT;
}

void setRTCMdata(uint8_t *rtcmData, uint32_t rtcmLen)
{
    memset(g_rtcmData.rtcmData, 0, rtcmLen);
    memcpy(g_rtcmData.rtcmData, rtcmData, rtcmLen);
    g_rtcmData.rtcmLen = rtcmLen;
    g_sandRtcmState = SAND_RTCM_DATA_OK;
}

C_dtuApi::C_dtuApi(/* args */)
{
    p_dtuDevice = NULL;
    spdlog::info("C_dtuApib, init");
    //m_recvBuf = (uint8_t*)malloc(512*(sizeof(uint8_t)));
}

C_dtuApi::~C_dtuApi()
{
    delete p_dtuDevice;
    p_dtuDevice = NULL;
    //delete []m_recvBuf;//释放数组空间 
}

// 初始化执行
int32_t C_dtuApi::initTask(CONFIG_DTU &config_dtu)
{
    spdlog::info("..............................................................");
    spdlog::info("C_dtuApi::initTask");
    task_name   = config_dtu.config_task.task_name;
    task_period = config_dtu.config_task.task_period;
    task_topic  = config_dtu.config_task.task_topic;
    spdlog::info("C_dtuApi task_name:'{}'", task_name);
    spdlog::info("C_dtuApi task_period:'{}'", task_period);
    spdlog::info("C_dtuApi task_topic:'{}'", task_topic);
    spdlog::info("C_dtuApi nband:          '{}'",config_dtu.config_serial_hardware.baud);
    spdlog::info("C_dtuApi ndata_bits:     '{}'",config_dtu.config_serial_hardware.data_bits);
    spdlog::info("C_dtuApi nserial_device: '{}'",config_dtu.config_serial_hardware.serial_device );
    spdlog::info("C_dtuApi nstop_bits:     '{}'",config_dtu.config_serial_hardware.stop_bits);
    spdlog::info("..............................................................");
    p_dtuDevice = new C_dtuDevice(config_dtu.config_serial_hardware.serial_device.c_str(), config_dtu.config_serial_hardware.baud);
    if(p_dtuDevice == NULL){
        return false;
    }
    return 0;
}


void sandRtcmData(RTCM_Date &p_rtcmData)
{
     for(int i=0; i<p_rtcmData.rtcmLen; i++){
        printf("%c ",p_rtcmData.rtcmData[i]);
    }
    printf("\r\n");
}

// 在while循环内执行
void C_dtuApi::runLoopTask(void)
{
    int32_t recv = 0;
    while (1)
    {
        recv = p_dtuDevice->getUsartData(p_dtuDevice->m_usratBuf); // 读取串口数据 按一帧数据返回
        if(recv == RECV_DONE){ // 如果接收到了一帧数据
            int32_t tempRecv = p_dtuDevice->parsingSerialData(p_dtuDevice->m_usratBuf); // 解析串口的数据
            if(tempRecv == PARSING_RTCM){
                setRTCMdata(p_dtuDevice->m_RtcmData.rtcmData, p_dtuDevice->m_RtcmData.rtcmLen);
                //sandRtcmData(p_dtuDevice->m_RtcmData);
            }
        }
        
    }
    //sandRtcmData(RTCM_Date *p_rtcmData);

    // 将RTCM数据打包
    // 发送RTCM数据发送出去
    // 将私有协议数据打包
    // 发送私有协议数据

    //std::thread thread_;
    // 等待接收到数据
    // 接收到数据后立即发送数据
    // if(接收到了数据)
    // 发送数据
    // 阻塞等待超时
    // 线程状态值更新
}


